package cn.dakaqi.services.impl;

import cn.dakaqi.dao.PinganVoucherCenterDao;
import cn.dakaqi.entities.PinganVoucherCenter;
import cn.dakaqi.services.PinganVoucherCenterService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author chunyang on 2016/4/19.
 */
@Component(value = "pinganVoucherCenterService")
@Transactional
public class PinganVoucherCenterServiceImpl implements PinganVoucherCenterService
{
    @Autowired
    PinganVoucherCenterDao pinganVoucherCenterDao;
    @Override
    public void save(PinganVoucherCenter pinganVoucherCenter) throws ServiceRuntimeException
    {
        this.pinganVoucherCenterDao.save(pinganVoucherCenter);
    }

    @Override
    public PinganVoucherCenter findByCardNum(String cardNum) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("cardNum", new SearchFilter("cardNum", SearchFilter.Operator.EQ, cardNum.trim()));
        Specification<PinganVoucherCenter> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganVoucherCenter.class);
        List<PinganVoucherCenter> list =  this.pinganVoucherCenterDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public Page<PinganVoucherCenter> findByMoney(String customer,Float money, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 400, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        if(!"all".equals(customer))
            filters.put("customer", new SearchFilter("customer", SearchFilter.Operator.EQ, customer));

        filters.put("money", new SearchFilter("money", SearchFilter.Operator.EQ, money));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curMonth = sdf.format(Calendar.getInstance().getTime());
        filters.put("createTime", new SearchFilter("createTime", SearchFilter.Operator.LIKE, curMonth));
        Specification<PinganVoucherCenter> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganVoucherCenter.class);
        return this.pinganVoucherCenterDao.findAll(spec,pageRequest);
    }

    public static void main(String[] args)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String curMonth = sdf.format(Calendar.getInstance().getTime());
        System.out.println(curMonth);
    }
}
